﻿using XMen.Framework.WPF;
using XMen.Framework.WPF.AyAnimate;
using XMen.Framework.WPF.Controls;
using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Markup;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace Demo.Controls.Controllers
{
   
    public partial class MyAyMessageBox : Window
    {

        private bool bool_0;

        public Action CloseOverride
        {
            get;
            set;
        }

        public MessageBoxResult MessageBoxResult
        {
            get;
            set;
        }
        public MyAyMessageBox(Window owner, string message, string title, MessageBoxButton button, AyMessageBoxImage icon, MessageBoxResult defaultResult, MessageBoxOptions options, Action closeAction, Action<AyFormInput> Init, int _1, Action<string> promtcallback = null)
        {
            MyAyMessageBox tag = this;
            this.InitializeComponent();
            base.Topmost = true;
            this.CloseOverride = closeAction;
            try
            {
                base.Owner = owner ?? System.Windows.Application.Current.MainWindow;
            }
            catch
            {
            }
            if (promtcallback == null)
            {
                this.method_1(button, defaultResult);
                this.method_8(icon);
                this.MessageText.Text = message;
            }
            else
            {
                AyFormInput ayFormInput = new AyFormInput()
                {
                    Height = 38,
                    MinWidth = 160,
                    Margin = new Thickness(24, 30, 28, 20),
                    Rule = "required",
                    VerticalAlignment = VerticalAlignment.Center,
                    HorizontalAlignment = HorizontalAlignment.Center
                };
                this.layout.Child = ayFormInput;
                if (Init.IsNotNull())
                {
                    Init(ayFormInput);
                }
                base.MinWidth = 400;
                Button button1 = new Button()
                {
                    Name = "okButton",
                  //  Content = "确认",
                    IsDefault = defaultResult == MessageBoxResult.OK,
                    Tag = MessageBoxResult.OK,
                    Width = 80,
                    Height = 32,
                    Margin = new Thickness(6, 0, 0, 0)
                };
                button1.SetResourceReference(ContentProperty, "MessageBox.OK");

                button1.Click += new RoutedEventHandler((object sender, RoutedEventArgs e) =>
                {
                    if (!ayFormInput.Validate())
                    {
                        ayFormInput.Focus();
                        return;
                    }
                    tag.MessageBoxResult = (MessageBoxResult)(sender as Button).Tag;
                    tag.method_7();
                    promtcallback(ayFormInput.Text);
                });
                this.ButtonsPanel.Children.Add(button1);
                Button button2 = new Button()
                {
                    Name = "cancelButton",
                   // Content = "取消",
                    IsDefault = defaultResult == MessageBoxResult.Cancel,
                    IsCancel = true,
                    Tag = MessageBoxResult.Cancel,
                    Width = 80,
                    Height = 32,
                    Margin = new Thickness(6, 0, 4, 0)
                };
                button2.SetResourceReference(ContentProperty, "MessageBox.Cancel");
                button2.Click += new RoutedEventHandler(this.method_6);
                this.ButtonsPanel.Children.Add(button2);
                this.ButtonsPanel.Margin = new Thickness(0, 0, 20, 16);
            }
            base.Title = title ?? "";
            base.Closed += new EventHandler(this.AyMessageBox_Closed);
            base.SourceInitialized += new EventHandler(this.AyMessageBox_SourceInitialized);
            this.method_0();
            base.Topmost = false;
        }

        public MyAyMessageBox(Window owner, string message, string title, MessageBoxButton button, string iconUrl, MessageBoxResult defaultResult, MessageBoxOptions options)
        {
            this.InitializeComponent();
            try
            {
                base.Owner = owner ?? System.Windows.Application.Current.MainWindow;
            }
            catch
            {
            }
            this.method_1(button, defaultResult);
            if (!string.IsNullOrEmpty(iconUrl))
            {
                this.ImagePlaceholder.Source = new BitmapImage(new Uri(iconUrl, UriKind.RelativeOrAbsolute));
            }
            else
            {
                this.ImagePlaceholder.Visibility = Visibility.Collapsed;
            }
            this.MessageText.Text = message;
            base.Title = title ?? "";
            base.SourceInitialized += new EventHandler(this.AyMessageBox_SourceInitialized);
            this.method_0();
        }

        private void AyMessageBox_Closed(object sender, EventArgs e)
        {
            if (base.Owner == null)
            {
                return;
            }
            base.Owner.Focus();
        }

        private void AyMessageBox_SourceInitialized(object sender, EventArgs e)
        {
            // HwndSource.FromHwnd((new WindowInteropHelper(this)).Handle).AddHook(new HwndSourceHook(MyAyMessageBox.WindowProc));
            IntPtr handle = (new WindowInteropHelper(this)).Handle;
            HwndSource source = HwndSource.FromHwnd(handle);
            if (source != null)
            {
                source.AddHook(WindowProc);
            }
        }

        private void closewindow_Click(object sender, RoutedEventArgs e)
        {
            this.method_7();
        }

        [DllImport("user32.dll", CharSet = CharSet.None, ExactSpelling = false)]
        private static extern bool GetCursorPos(ref Point point_0);

        [DllImport("user32", CharSet = CharSet.None, ExactSpelling = false)]
        internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO monitorinfo0);



        private void method_0()
        {
            AyAniScale ayAniScale = new AyAniScale(this.body, () =>
            {
            })
            {
                AnimateSpeed = 450
            };
            ayAniScale.ScaleXFrom = (new double?(0));
            ayAniScale.ScaleYFrom = (new double?(0));
            ayAniScale.ScaleXTo = new double?(1f);
            ayAniScale.ScaleYTo = new double?(1f);
            ayAniScale.EasingFunction = new CubicEase()
            {
                EasingMode = EasingMode.EaseOut
            };
            ayAniScale.Animate().End();
        }

        private void method_1(MessageBoxButton messageBoxButton_0, MessageBoxResult messageBoxResult_1)
        {
            switch (messageBoxButton_0)
            {
                case MessageBoxButton.OK:
                    {
                        this.ButtonsPanel.Children.Add(this.method_2(messageBoxResult_1));
                        return;
                    }
                case MessageBoxButton.OKCancel:
                    {
                        this.ButtonsPanel.Children.Add(this.method_2(messageBoxResult_1));
                        this.ButtonsPanel.Children.Add(this.method_3(messageBoxResult_1));
                        return;
                    }
                case MessageBoxButton.YesNoCancel:
                    {
                        this.ButtonsPanel.Children.Add(this.method_4(messageBoxResult_1));
                        this.ButtonsPanel.Children.Add(this.method_5(messageBoxResult_1));
                        this.ButtonsPanel.Children.Add(this.method_3(messageBoxResult_1));
                        return;
                    }
                case MessageBoxButton.YesNo:
                    {
                        this.ButtonsPanel.Children.Add(this.method_4(messageBoxResult_1));
                        this.ButtonsPanel.Children.Add(this.method_5(messageBoxResult_1));
                        return;
                    }
                default:
                    {
                        throw new ArgumentOutOfRangeException("button");
                    }
            }
        }

        private Button method_2(MessageBoxResult messageBoxResult_1)
        {
            Button button = new Button()
            {
                Name = "okButton",
                Content = "确认",
                IsDefault = messageBoxResult_1 == MessageBoxResult.OK,
                Tag = MessageBoxResult.OK,
                Width = 80,
                Height = 32,
                Margin = new Thickness(6, 0, 0, 0)
            };
            button.SetResourceReference(ContentProperty, "MessageBox.OK");
            button.Click += new RoutedEventHandler(this.method_6);
            return button;
        }

        private Button method_3(MessageBoxResult messageBoxResult_1)
        {
            Button button = new Button()
            {
                Name = "cancelButton",
               // Content = "取消",
                IsDefault = messageBoxResult_1 == MessageBoxResult.Cancel,
                IsCancel = true,
                Tag = MessageBoxResult.Cancel,
                Width = 80,
                Height = 32,
                Margin = new Thickness(6, 0, 0, 0)
            };
            button.SetResourceReference(ContentProperty, "MessageBox.Cancel");
            button.Click += new RoutedEventHandler(this.method_6);
            return button;
        }

        private Button method_4(MessageBoxResult messageBoxResult_1)
        {
            Button button = new Button()
            {
                Name = "yesButton",
                //Content = "是",
                IsDefault = messageBoxResult_1 == MessageBoxResult.Yes,
                Tag = MessageBoxResult.Yes,
                Width = 80,
                Height = 32,
                Margin = new Thickness(6, 0, 0, 0)
            };
            button.SetResourceReference(ContentProperty, "MessageBox.Yes");
            button.Click += new RoutedEventHandler(this.method_6);
            return button;
        }

        private Button method_5(MessageBoxResult messageBoxResult_1)
        {
            Button button = new Button()
            {
                Name = "noButton",
                //Content = "否",
                IsDefault = messageBoxResult_1 == MessageBoxResult.No,
                Tag = MessageBoxResult.No,
                Width = 80,
                Height = 32,
                Margin = new Thickness(6, 0, 0, 0)
            };
            button.SetResourceReference(ContentProperty, "MessageBox.No");
            button.Click += new RoutedEventHandler(this.method_6);
            return button;
        }

        private void method_6(object sender, RoutedEventArgs e)
        {
            this.MessageBoxResult = (MessageBoxResult)(sender as Button).Tag;
            this.method_7();
        }

        private void method_7()
        {
            if (this.CloseOverride.IsNotNull())
            {
                this.CloseOverride();
            }
            (new AyAniZoomBounceOut(this.body, () => base.Close())).Animate().End();
        }

        private void method_8(AyMessageBoxImage ayMessageBoxImage_0)
        {
            if (ayMessageBoxImage_0 > AyMessageBoxImage.Error)
            {
                if (ayMessageBoxImage_0 == AyMessageBoxImage.Question)
                {
                    this.ImagePlaceholder.Source = AyIcons.Question();
                    return;
                }
                if (ayMessageBoxImage_0 == AyMessageBoxImage.Exclamation)
                {
                    this.ImagePlaceholder.Source = AyIcons.Warning();
                    return;
                }
                if (ayMessageBoxImage_0 == AyMessageBoxImage.Asterisk)
                {
                    this.ImagePlaceholder.Source = AyIcons.Information();
                    return;
                }
            }
            else
            {
                switch (ayMessageBoxImage_0)
                {
                    case AyMessageBoxImage.None:
                        {
                            this.ImagePlaceholder.Visibility = Visibility.Collapsed;
                            return;
                        }
                    case AyMessageBoxImage.Delete:
                        {
                            this.ImagePlaceholder.Source = AyIcons.Delete();
                            return;
                        }
                    case AyMessageBoxImage.Right:
                        {
                            this.ImagePlaceholder.Source = AyIcons.Right();
                            return;
                        }
                    default:
                        {
                            if (ayMessageBoxImage_0 == AyMessageBoxImage.Error)
                            {
                                this.ImagePlaceholder.Source = AyIcons.Error();
                                return;
                            }
                            break;
                        }
                }
            }
            throw new ArgumentOutOfRangeException("icon");
        }

       
        [DllImport("user32", CharSet = CharSet.None, ExactSpelling = false)]
        internal static extern IntPtr MonitorFromWindow(IntPtr intptr_0, int int_0);

        public static MessageBoxResult Promt(Action<string> callBack, string title = "")
        {
            return MyAyMessageBox.smethod_0(null, null, callBack, title, MessageBoxButton.OKCancel, AyMessageBoxImage.None, MessageBoxResult.OK, MessageBoxOptions.None);
        }

        public static MessageBoxResult Promt(Action<AyFormInput> init, Action<string> callBack, string title = "")
        {
            return MyAyMessageBox.smethod_0(null, init, callBack, title, MessageBoxButton.OKCancel, AyMessageBoxImage.None, MessageBoxResult.OK, MessageBoxOptions.None);
        }

        public static MessageBoxResult Promt(Window owner, Action<AyFormInput> init, Action<string> callBack, string title = "")
        {
            return MyAyMessageBox.smethod_0(owner, init, callBack, title, MessageBoxButton.OKCancel, AyMessageBoxImage.None, MessageBoxResult.OK, MessageBoxOptions.None);
        }

        public static MessageBoxResult Show(string message)
        {
            return MyAyMessageBox.Show(null, message, null, MessageBoxButton.OK, AyMessageBoxImage.None, MessageBoxResult.Yes, MessageBoxOptions.RightAlign);
        }

        public static MessageBoxResult Show(string message, string title)
        {
            return MyAyMessageBox.Show(null, message, title, MessageBoxButton.OK, AyMessageBoxImage.None, MessageBoxResult.Yes, MessageBoxOptions.RightAlign);
        }

        public static MessageBoxResult Show(string message, string title = "", MessageBoxButton button = 0, AyMessageBoxImage icon = 0, MessageBoxResult defaultResult = 0, MessageBoxOptions options = 0)
        {
            return MyAyMessageBox.Show(null, message, title, button, icon, defaultResult, options);
        }

        public static MessageBoxResult Show(string message, MessageBoxButton button = 0, AyMessageBoxImage icon = 0, MessageBoxResult defaultResult = 0, MessageBoxOptions options = 0)
        {
            return MyAyMessageBox.Show(message, string.Empty, button, icon, defaultResult, options);
        }

        public static MessageBoxResult Show(Window owner, string message, MessageBoxButton button = 0, AyMessageBoxImage icon = 0, MessageBoxResult defaultResult = 0, MessageBoxOptions options = 0)
        {
            return MyAyMessageBox.Show(owner, message, string.Empty, button, icon, defaultResult, options);
        }

        public static MessageBoxResult Show(Window owner, string message, Action closeAction, string title = "", MessageBoxButton button = 0, AyMessageBoxImage icon = 0, MessageBoxResult defaultResult = 0, MessageBoxOptions options = 0)
        {
            return (MessageBoxResult)System.Windows.Application.Current.Dispatcher.Invoke(new Func<MessageBoxResult>(() =>
            {
                MyAyMessageBox ayMessageBox = new MyAyMessageBox(owner, message, title, button, icon, defaultResult, options, closeAction, null, 1, null);
                ayMessageBox.ShowDialog();
                return ayMessageBox.MessageBoxResult;
            }), new object[0]);
        }

        public static MessageBoxResult Show(Window owner, string message, string title = "", MessageBoxButton button = 0, AyMessageBoxImage icon = 0, MessageBoxResult defaultResult = 0, MessageBoxOptions options = 0)
        {
            return (MessageBoxResult)System.Windows.Application.Current.Dispatcher.Invoke(new Func<MessageBoxResult>(() =>
            {
                MyAyMessageBox ayMessageBox = new MyAyMessageBox(owner, message, title, button, icon, defaultResult, options, null, null, 1, null);
                ayMessageBox.ShowDialog();
                return ayMessageBox.MessageBoxResult;
            }), new object[0]);
        }

        public static MessageBoxResult ShowCus(string message, string title = "", string iconUri = "", MessageBoxButton button = 0, MessageBoxResult defaultResult = 0)
        {
            return MyAyMessageBox.ShowCus(null, message, title, iconUri, button, defaultResult);
        }

        public static MessageBoxResult ShowCus(Window owner, string message, string title = "", string iconUri = "", MessageBoxButton button = 0, MessageBoxResult defaultResult = 0)
        {
            return (MessageBoxResult)System.Windows.Application.Current.Dispatcher.Invoke(new Func<MessageBoxResult>(() =>
            {
                MyAyMessageBox ayMessageBox = new MyAyMessageBox(owner, message, title, button, iconUri, defaultResult, MessageBoxOptions.None);
                ayMessageBox.ShowDialog();
                return ayMessageBox.MessageBoxResult;
            }), new object[0]);
        }

        public static MessageBoxResult ShowDelete(string message, string title = "", MessageBoxOptions options = 0)
        {
            return MyAyMessageBox.ShowDelete(null, message, title, options);
        }

        public static MessageBoxResult ShowDelete(Window owner, string message, string title = "", MessageBoxOptions options = 0)
        {
            return MyAyMessageBox.Show(owner, message, title, MessageBoxButton.OKCancel, AyMessageBoxImage.Delete, MessageBoxResult.OK, options);
        }

        public static MessageBoxResult ShowError(string message, string title = "", bool showCancel = false, MessageBoxOptions options = 0)
        {
            return MyAyMessageBox.ShowError(null, message, title, showCancel, options);
        }

        public static MessageBoxResult ShowError(Window owner, string message, string title = "", bool showCancel = false, MessageBoxOptions options = 0)
        {
            return MyAyMessageBox.Show(owner, message, title, (showCancel ? MessageBoxButton.OKCancel : MessageBoxButton.OK), AyMessageBoxImage.Error, MessageBoxResult.OK, options);
        }

        public static MessageBoxResult ShowInformation(string message, string title = "", bool showCancel = false, MessageBoxOptions options = 0)
        {
            return MyAyMessageBox.ShowInformation(null, message, title, showCancel, options);
        }

        public static MessageBoxResult ShowInformation(Window owner, string message, string title = "", bool showCancel = false, MessageBoxOptions options = 0)
        {
            return MyAyMessageBox.Show(owner, message, title, (showCancel ? MessageBoxButton.OKCancel : MessageBoxButton.OK), AyMessageBoxImage.Asterisk, MessageBoxResult.OK, options);
        }

        public static MessageBoxResult ShowQuestion(string message, string title = "", bool showCancel = false, MessageBoxOptions options = 0)
        {
            return MyAyMessageBox.ShowQuestion(null, message, title, showCancel, options);
        }

        public static MessageBoxResult ShowQuestion(Window owner, string message, string title = "", bool showCancel = false, MessageBoxOptions options = 0)
        {
            return MyAyMessageBox.Show(owner, message, title, (showCancel ? MessageBoxButton.YesNoCancel : MessageBoxButton.YesNo), AyMessageBoxImage.Question, MessageBoxResult.Yes, options);
        }

        public static MessageBoxResult ShowQuestion(Window owner, string message, Action closeAction, string title = "", bool showCancel = false, MessageBoxOptions options = 0)
        {
            return MyAyMessageBox.Show(owner, message, closeAction, title, (showCancel ? MessageBoxButton.YesNoCancel : MessageBoxButton.YesNo), AyMessageBoxImage.Question, MessageBoxResult.Yes, options);
        }

        public static MessageBoxResult ShowQuestionOkCancel(string message, string title = "", MessageBoxOptions options = 0)
        {
            return MyAyMessageBox.ShowQuestionOkCancel(null, message, title, options);
        }

        public static MessageBoxResult ShowQuestionOkCancel(Window owner, string message, string title = "", MessageBoxOptions options = 0)
        {
            return MyAyMessageBox.Show(owner, message, title, MessageBoxButton.OKCancel, AyMessageBoxImage.Question, MessageBoxResult.OK, options);
        }

        public static MessageBoxResult ShowRight(string message, string title = "", bool showCancel = false, MessageBoxOptions options = 0)
        {
            return MyAyMessageBox.ShowRight(null, message, title, showCancel, options);
        }

        public static MessageBoxResult ShowRight(Window owner, string message, string title = "", bool showCancel = false, MessageBoxOptions options = 0)
        {
            return MyAyMessageBox.Show(owner, message, title, (showCancel ? MessageBoxButton.OKCancel : MessageBoxButton.OK), AyMessageBoxImage.Right, MessageBoxResult.OK, options);
        }

        public static MessageBoxResult ShowWarning(string message, string title = "", bool showCancel = false, MessageBoxOptions options = 0)
        {
            return MyAyMessageBox.ShowWarning(null, message, title, showCancel, options);
        }

        public static MessageBoxResult ShowWarning(Window owner, string message, string title = "", bool showCancel = false, MessageBoxOptions options = 0)
        {
            return MyAyMessageBox.Show(owner, message, title, (showCancel ? MessageBoxButton.OKCancel : MessageBoxButton.OK), AyMessageBoxImage.Exclamation, MessageBoxResult.OK, options);
        }

        internal static MessageBoxResult smethod_0(Window window_0, Action<AyFormInput> action_1, Action<string> action_2, string string_0 = "", MessageBoxButton messageBoxButton_0 = MessageBoxButton.OKCancel, AyMessageBoxImage ayMessageBoxImage_0 = 0, MessageBoxResult messageBoxResult_1 = 0, MessageBoxOptions messageBoxOptions_0 = 0)
        {
            MessageBoxResult messageBoxResult;
            try
            {
                messageBoxResult = (MessageBoxResult)System.Windows.Application.Current.Dispatcher.Invoke(new Func<MessageBoxResult>(() =>
                {
                    MyAyMessageBox ayMessageBox = new MyAyMessageBox(window_0, null, string_0, messageBoxButton_0, ayMessageBoxImage_0, messageBoxResult_1, messageBoxOptions_0, null, action_1, 1, action_2);
                    ayMessageBox.ShowDialog();
                    return ayMessageBox.MessageBoxResult;
                }), new object[0]);
            }
            catch
            {
                messageBoxResult = MessageBoxResult.None;
            }
            return messageBoxResult;
        }

        private static IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == 0x0024)
            {
                WmGetMinMaxInfo(hwnd, lParam);
                handled = true;
            }
            return (IntPtr)0;
        }

        private static void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam)
        {
            MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
            IntPtr monitor = MyAyMessageBox.MonitorFromWindow(hwnd, 2);
            if (monitor  != IntPtr.Zero)
            {
                MONITORINFO monitorinfo = new MONITORINFO();
                GetMonitorInfo(monitor , monitorinfo);
                RECT rcWorkArea= monitorinfo.rcWork;
                RECT rcMonitorArea = monitorinfo.rcMonitor; 
                mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
                mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);
                mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
                mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
            }
            Marshal.StructureToPtr(mmi, lParam, true);
        }

    }
}
